package com.example.shopsystem.service;

import com.example.shopsystem.dao.BusinessMapper;
import com.example.shopsystem.dao.GoodMapper;
import com.example.shopsystem.dao.OrderMapper;
import com.example.shopsystem.dao.UserMapper;
import com.example.shopsystem.entity.*;
import com.example.shopsystem.entity.vo.OrderDetailVo;
import com.example.shopsystem.entity.vo.OrderVo;
import com.example.shopsystem.service.Impl.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    GoodMapper goodMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    BusinessMapper businessMapper;

    @Override
    public void returnGoods(int orderId, int goodId) {

    }


    @Override
    public void viewAllSuccessfulOrders() {

    }

    @Override
    public List<OrderDeail> queryOrderDeail(String orderId) {

        return orderMapper.queryOrderDeail(orderId);
    }

    @Transactional
    @Override
    public Boolean insertOrder(Order order) {
        List<ShopCar> shopCarList = userMapper.viewShopCar(order.getUId());
        if (shopCarList == null) {
            return false;
        }
        double money = 0.00;
        for (ShopCar shopCar :
                shopCarList) {
            money = shopCar.getNowMoney() + money;
        }
        order.setActualPayment(money);
        System.out.println(order);
        orderMapper.insertOrder(order);

        System.out.println(order);

        for (ShopCar shopCar :
                shopCarList) {
            orderMapper.insertOrder_detailed(shopCar, order.getId());
        }
        return true;
    }

    @Override
    public Boolean updateOrder(OrderVo orderVo) {
        List<Order> list = orderMapper.queryUserOrder(orderVo.getUser().getId());
        if (list == null) {
            return false;
        }
        for (Order order :
                list) {
            if (order.getId() == orderVo.getOrder().getId()) {
                if (!orderMapper.updateOrder(orderVo.getOrder())) {
                    return false;
                } else return true;
            }
        }


        return false;
    }

    @Override
    public Boolean updateOrderState(Order order) {
        if (!orderMapper.updateOrder(order)) {
            return false;
        }
        ;
        return true;
    }

    @Override
    public List<OrderDetailVo> ViewSales(String beginTime, String dieTime) {//查询订单

        List<Order> orderList = orderMapper.queryOrder(beginTime, dieTime);
        return OrderDto(orderList);
    }

    public  List<OrderDetailVo> OrderDto(List<Order> orderList) {//订单组合

        List<OrderDetailVo> orderDtoList = new LinkedList<>();
        for (Order order : orderList) {
            OrderDetailVo orderDto = new OrderDetailVo();
            orderDto.setOrder(order);
            User user = userMapper.queryUser(order.getUId());
            user.setBalance(0);
            user.setPwd(null);
            user.setMoneyPwd(null);
            orderDto.setUser(user);
            Business business = businessMapper.queryBusiness(order.getBusId());
            business.setMoneyPwd(0);
            business.setPwd(null);
            business.setBalance(0);
            orderDto.setBusiness(business);
            orderDto.setOrderDeailList(orderMapper.queryOrderDeail(String.valueOf(order.getId())));
            orderDtoList.add(orderDto);
        }

        return orderDtoList;
    }

    @Override
    public List<OrderDetailVo> ViewUserSales(String userId) {//用户查询订单
        List<Order> orderList = orderMapper.queryUserOrder(userId);
//        System.out.println(orderList+"orderList");
        return OrderDto(orderList);
    }
}
